﻿using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using ContractManagementSystem.Entity;

namespace ContractManagementSystem.DataAccess
{
    public class ContractTypeDataAccess : LogDataAccess
    {
        private const string UserName = "Admin";

        public int Insert(ContractTypeEntity contractTypeEntity)
        {
            if (!string.IsNullOrEmpty(ErrorMessage))
            {
                //log error
                WriteError(ErrorMessage, UserName);
                return 0;
            }

            int result = 0;
            try
            {
                //log event          
                WriteEvent("Inserting...", UserName);
                using (var sqlCommand = new SqlCommand("sp_ContractType_Insert", CreateConnection()))
                {
                    sqlCommand.CommandType = CommandType.StoredProcedure;
                    sqlCommand.Parameters.AddWithValue("@name", contractTypeEntity.Name);
                    sqlCommand.Parameters.AddWithValue("@description", contractTypeEntity.Description);

                    result = sqlCommand.ExecuteNonQuery();
                    ErrorMessage = null;
                }
            }
            catch (SqlException sqlException)
            {
                ErrorMessage = sqlException.Message;

                //log event
                WriteEvent("Insert fail!", UserName);

                //log error
                WriteError(ErrorMessage, UserName);

                return result;
            }
            finally
            {
                CloseConnection();
            }

            //log event
            WriteEvent("Insert success!", UserName);

            return result;
        }

        public int Update(ContractTypeEntity entity)
        {
            if (!string.IsNullOrEmpty(ErrorMessage))
            {
                //log error
                WriteError(ErrorMessage, UserName);
                return 0;
            }

            int result = 0;
            try
            {
                //log event                
                WriteEvent("Updating...", UserName);
                using (var sqlCommand = new SqlCommand("sp_ContractType_Update", CreateConnection()))
                {
                    sqlCommand.CommandType = CommandType.StoredProcedure;
                    sqlCommand.Parameters.AddWithValue("@id", entity.ContractTypeId);
                    sqlCommand.Parameters.AddWithValue("@name", entity.Name);
                    sqlCommand.Parameters.AddWithValue("@description", entity.Description);

                    result = sqlCommand.ExecuteNonQuery();
                    ErrorMessage = null;
                }
            }
            catch (SqlException sqlException)
            {
                ErrorMessage = sqlException.Message;

                //log event
                WriteEvent("Update fail!", UserName);

                //log error
                WriteError(ErrorMessage, UserName);

                return result;
            }
            finally
            {
                CloseConnection();
            }

            //log event
            WriteEvent("Update success!", UserName);
            return result;
        }

        public int Delete(string id)
        {
            if (!string.IsNullOrEmpty(ErrorMessage))
            {
                //log error
                WriteError(ErrorMessage, UserName);
                return 0;
            }

            int result = 0;
            try
            {
                //log event                
                WriteEvent("Deleting...", UserName);
                using (var sqlCommand = new SqlCommand("sp_ContractType_Delete", CreateConnection()))
                {
                    sqlCommand.CommandType = CommandType.StoredProcedure;
                    sqlCommand.Parameters.Add(new SqlParameter("@id", id));

                    result = sqlCommand.ExecuteNonQuery();
                    ErrorMessage = null;
                }
            }
            catch (SqlException sqlException)
            {
                ErrorMessage = sqlException.Message;

                //log event
                WriteEvent("Delete fail!", UserName);

                //log error
                WriteError(ErrorMessage, UserName);

                return result;
            }
            finally
            {
                CloseConnection();
            }

            //log event
            WriteEvent("Delete success!", UserName);

            return result;
        }

        public new DataTable FetchAll()
        {
            if (!string.IsNullOrEmpty(ErrorMessage))
            {
                //log error
                WriteError(ErrorMessage, UserName);
                return null;
            }

            DataTable result = null;
            try
            {
                //log event                
                WriteEvent("Selecting...", UserName);
                using (var sqlCommand = new SqlCommand("sp_ContractType_Select", CreateConnection()))
                {
                    sqlCommand.CommandType = CommandType.StoredProcedure;

                    var adapter = new SqlDataAdapter(sqlCommand);
                    result = new DataTable { Locale = CultureInfo.CurrentCulture };
                    adapter.Fill(result);
                    ErrorMessage = null;
                    //log event
                    WriteEvent("Select success!", UserName);
                }
            }
            catch (SqlException sqlException)
            {
                ErrorMessage = sqlException.Message;

                //log event
                WriteEvent("Select fail!", UserName);

                //log error
                WriteError(ErrorMessage, UserName);

                return result;
            }
            finally
            {
                CloseConnection();
            }

            return result;
        }
    }
}
